{% extends "base-user.html" %}

{% block css%}    
{% endblock %}
{% block js%}    
{% endblock %}


{% block on_ready%}    
{% endblock %}

{% block content %} 
<div class="row">      
    <div class="col-md-12">
        <div class="box box-solid box-navy ">
            <div class="box-header">
                <i class="fa fa-h-square"></i>
                <h3 class="box-title">Document</h3>

            </div><!-- /.box-header -->
            <div class="box-body  ">

<a href="{% url 'PiApp.views.home_help_en_view' %}" style="float:right;">EN</a>





<div class="md-section-divider"></div>

<div class="md-section-divider"></div>

<h1 id="上传数据" data-anchor-id="g86a">上传数据</h1>

<p data-anchor-id="2rd7"><strong>值类型</strong>：</p>

<ul data-anchor-id="r6lx">
<li>msg：'ok'  or  'fail'</li>
<li>key：datetime, such as 2012-03-15T16:13:14  or  <br>
2012-03-15 16:13:14。</li>
<li>value： url(as '/pibox_upload/xxxx')  or  float</li>
</ul>

<div class="md-section-divider"></div>

<h2 id="开关传感器" data-anchor-id="q2sr">开关传感器</h2>

<table data-anchor-id="f64t" class="table table-striped-white table-bordered">
<thead>
<tr>
 <th>url</th>
 <th style="text-align:right;">作用</th>
 <th style="text-align:center;">方法</th>
 <th style="text-align:center;">请求参数</th>
 <th style="text-align:center;">返回参数</th>
</tr>
</thead>
<tbody><tr>
 <td>/API/sensor/{sensor_id}/datapoint/</td>
 <td style="text-align:right;">修改</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">value<code>int</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/get/</td>
 <td style="text-align:right;">查询</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;"></td>
 <td style="text-align:center;">{'msg', 'value'}</td>
</tr>
</tbody></table>


<div class="md-section-divider"></div>

<h2 id="数值传感器" data-anchor-id="52qw">数值传感器</h2>

<table data-anchor-id="svgz" class="table table-striped-white table-bordered">
<thead>
<tr>
 <th>url</th>
 <th style="text-align:center;">作用</th>
 <th style="text-align:center;">方法</th>
 <th style="text-align:center;">请求参数</th>
 <th style="text-align:center;">返回参数</th>
</tr>
</thead>
<tbody><tr>
 <td>/API/sensor/{sensor_id}/datapoint/</td>
 <td style="text-align:center;">新增</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code>，value<code>float</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/</td>
 <td style="text-align:center;">新增</td>
 <td style="text-align:center;">POST</td>
 <td style="text-align:center;">[{'key' : <code>datetime</code>, 'value' : <code>float</code>}]</td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/get/</td>
 <td style="text-align:center;">查询</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code></td>
 <td style="text-align:center;">{'msg', 'value'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/edit/</td>
 <td style="text-align:center;">修改</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code>，value<code>float</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/remove/</td>
 <td style="text-align:center;">删除</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/history/</td>
 <td style="text-align:center;">历史（时间段）</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">start<code>datetime</code>, end<code>datetime</code>, interval<code>int(seconds）</code></td>
 <td style="text-align:center;">{'msg'， 'datapoint'=[{'value','key'}]</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/history/</td>
 <td style="text-align:center;">历史（返回最新前二十条）</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;"></td>
 <td style="text-align:center;">{'msg'， 'datapoint'=[{'value','key'}]}</td>
</tr>
</tbody></table>


<p data-anchor-id="i89e">json data could be like this：</p>

<div class="md-section-divider"></div>

<pre class="prettyprint linenums prettyprinted" data-anchor-id="it3n" style=""><ol class="linenums"><li class="L0"><code><span class="pln">    </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">      </span><span class="str">"key"</span><span class="pun">:</span><span class="str">"2012-03-15T16:13:14"</span><span class="pun">,</span></code></li><li class="L2"><code><span class="pln">      </span><span class="str">"value"</span><span class="pun">:</span><span class="lit">294.34</span></code></li><li class="L3"><code><span class="pln">    </span><span class="pun">}</span></code></li></ol></pre>

<p data-anchor-id="uufi">or</p>

<div class="md-section-divider"></div>

<pre class="prettyprint linenums prettyprinted" data-anchor-id="3w5d" style=""><ol class="linenums"><li class="L0"><code><span class="pln">    </span><span class="pun">[</span></code></li><li class="L1"><code><span class="pln">      </span><span class="pun">{</span><span class="str">"key"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"2012-06-15T14:00:00"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">:</span><span class="lit">315.01</span><span class="pun">},</span></code></li><li class="L2"><code><span class="pln">      </span><span class="pun">{</span><span class="str">"key"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"2012-06-15T14:00:10"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">:</span><span class="lit">316.23</span><span class="pun">},</span></code></li><li class="L3"><code><span class="pln">      </span><span class="pun">{</span><span class="str">"key"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"2012-06-15T14:00:20"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">:</span><span class="lit">317.26</span><span class="pun">},</span></code></li><li class="L4"><code><span class="pln">      </span><span class="pun">{</span><span class="str">"key"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"2012-06-15T14:00:30"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">:</span><span class="lit">318</span><span class="pun">},</span></code></li><li class="L5"><code><span class="pln">      </span><span class="pun">{</span><span class="str">"key"</span><span class="pun">:</span><span class="pln"> </span><span class="str">"2012-06-15T14:00:40"</span><span class="pun">,</span><span class="pln"> </span><span class="str">"value"</span><span class="pun">:</span><span class="lit">317</span><span class="pun">}</span></code></li><li class="L6"><code><span class="pln">    </span><span class="pun">]</span></code></li></ol></pre>

<div class="md-section-divider"></div>

<h2 id="图像传感器" data-anchor-id="b2eo">图像传感器</h2>

<table data-anchor-id="mj4i" class="table table-striped-white table-bordered">
<thead>
<tr>
 <th>url</th>
 <th style="text-align:center;">作用</th>
 <th style="text-align:center;">方法</th>
 <th style="text-align:center;">请求参数</th>
 <th style="text-align:center;">返回参数</th>
</tr>
</thead>
<tbody><tr>
 <td>/API/sensor/{sensor_id}/datapoint/</td>
 <td style="text-align:center;">新增</td>
 <td style="text-align:center;">POST</td>
 <td style="text-align:center;">key<code>datetime</code>，value<code>file</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/get/</td>
 <td style="text-align:center;">查询</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code></td>
 <td style="text-align:center;">{'msg', 'value<code>url</code>'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/edit/</td>
 <td style="text-align:center;">修改</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code>，value<code>file</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/remove/</td>
 <td style="text-align:center;">删除</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">key<code>datetime</code></td>
 <td style="text-align:center;">{'msg'}</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/history/</td>
 <td style="text-align:center;">历史（时间段）</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;">start<code>datetime</code>, end<code>datetime</code>, interval<code>int(seconds）</code></td>
 <td style="text-align:center;">{'msg'， 'datapoint'=[{'value<code>url</code>','key'}]</td>
</tr>
<tr>
 <td>/API/sensor/{sensor_id}/datapoint/history/</td>
 <td style="text-align:center;">历史（返回最新前二十条）</td>
 <td style="text-align:center;">GET</td>
 <td style="text-align:center;"></td>
 <td style="text-align:center;">{'msg'， 'datapoint'=[{'value<code>url</code>','key'}]}</td>
</tr>
</tbody></table>


<p data-anchor-id="mpnj">ret can be like this</p>

<div class="md-section-divider"></div>

<pre class="prettyprint linenums prettyprinted" data-anchor-id="8gcc" style=""><ol class="linenums"><li class="L0"><code><span class="pln">    </span><span class="pun">{</span></code></li><li class="L1"><code><span class="pln">      </span><span class="str">"key"</span><span class="pun">:</span><span class="str">"2012-03-15T16:13:14"</span><span class="pun">,</span></code></li><li class="L2"><code><span class="pln">      </span><span class="str">"value"</span><span class="pun">:</span><span class="str">"/media/pibox_upload/pic_datapoint/111.png"</span></code></li><li class="L3"><code><span class="pln">    </span><span class="pun">}</span></code></li></ol></pre>

<p data-anchor-id="oohn">注意返回的都是url，具体图片再从url读</p>

<div class="md-section-divider"></div>

<h2 id="example" data-anchor-id="3z2x">Example</h2>

<p data-anchor-id="6ojs">在sh/datapoint_tools下有使用urllib的数据测试脚本，可以用来增删数据，也可以用来做例子。 <br>
<a href="https://github.com/wzyy2/PiBox/tree/master/PiBox/sh/datapoint_tools/switch.py" target="_blank">开关传感的读写脚本</a> <br>
<a href="https://github.com/wzyy2/PiBox/tree/master/PiBox/sh/datapoint_tools/num.py" target="_blank">数据传感的CURD脚本</a> <br>
<a href="https://github.com/wzyy2/PiBox/tree/master/PiBox/sh/datapoint_tools/pic.py" target="_blank">图片传感的CURD脚本</a> <br>
使用办法如下</p>

<div class="md-section-divider"></div>

<pre class="prettyprint linenums prettyprinted" data-anchor-id="3qud" style=""><ol class="linenums"><li class="L0"><code><span class="pun">输入</span><span class="pln">python </span><span class="kwd">switch</span><span class="pun">.</span><span class="pln">py</span></code></li><li class="L1"><code><span class="pun">输出</span><span class="lit">1.write</span><span class="pln">   </span><span class="lit">2.read</span><span class="pln"> </span><span class="pun">选择</span><span class="pln"> </span><span class="lit">1</span></code></li><li class="L2"><code><span class="pun">输出</span><span class="pln">domain</span><span class="pun">(</span><span class="kwd">as</span><span class="pln"> </span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">10.106</span><span class="pun">:</span><span class="lit">8000</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="pun">输入</span><span class="lit">192.168</span><span class="pun">.</span><span class="lit">10.106</span><span class="pun">:</span><span class="lit">8000</span></code></li><li class="L3"><code><span class="pun">输出</span><span class="pln">sensor_id</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="pun">输入</span><span class="pln"> </span><span class="lit">1</span></code></li><li class="L4"><code><span class="pun">输出</span><span class="pln">value</span><span class="pun">(</span><span class="kwd">int</span><span class="pun">)</span><span class="pln"> </span><span class="pun">:</span><span class="pln"> </span><span class="pun">输入</span><span class="pln"> </span><span class="lit">1</span></code></li></ol></pre>

<p data-anchor-id="tzn4">只要对这些脚本稍加修改，另外加上一些python代码就可以通过以下思路，实现自己的本地智能家居： <br>
* 通过串口等通讯模块，关联单片机，然后关联上native yeelink。 <br>
* 通过驱动读取树莓派所接模块的数据，然后上传数据到native yeelink。</p>

<div class="md-section-divider"></div>

<h1 id="callback" data-anchor-id="1bga">CallBack</h1>

<p data-anchor-id="wnw8">Callback file是传感器满足条件后被调用的python代码文件名<code>比如gpio.py</code>，被调用的文件需要放在PiBox/sh/callback/下(重启有效)。 <br>
不使用的话只需提供不存在的文件名或者保持为空即可。</p>

<p data-anchor-id="596v">对开关传感器来说，每一次开关数值点发生修改，callback_file都会被按如下格式调用</p>

<pre data-anchor-id="txbt"><code>    python callback_file sensor.name swtich_status(1 or 0)
</code></pre>

<p data-anchor-id="qgk6">对数值传感器来说,每一次新增或者修改的数值都会检查条件，如果满足条件，callback_file都会被按如下格式调用</p>

<pre data-anchor-id="2k15"><code>    python callback_file sensor.name value（溢出的数值） key（时间）
</code></pre>

<div class="md-section-divider"></div>

<h2 id="case" data-anchor-id="4pdr">Case</h2>

<p data-anchor-id="jjz9">在sh/callback下有几个文件实例可以直接使用</p>

<div class="md-section-divider"></div>

<h3 id="gpiopy开关" data-anchor-id="wefi">gpio.py（开关）</h3>

<pre data-anchor-id="s032"><code>import os,sys

cwd = os.path.dirname(os.path.abspath(__file__)) + '/..' + '/..'
sys.path.append(os.path.join(cwd, 'PiHome'))

from common.driver import linux_gpio

print 'Sensor:', str(sys.argv[1])
print 'Value:', str(sys.argv[2])

GPIO_NUM = 23

print 'GPIO_BCM_NUM:', GPIO_NUM

gpio = linux_gpio.gpio(GPIO_NUM)
gpio.gpio_export()
gpio.write_gpio_direction('out')
gpio.write_gpio_value(sys.argv[2])
</code></pre>

<p data-anchor-id="5rdx">在需要的开关传感器下填入gpio.py,将文件的GPIO_NUM改成所需的gpio编号，每次开关状态改变，就会带动gpio改变。 <br>
直接使用这个脚本就可以通过web来控制简单电器</p>

<div class="md-section-divider"></div>

<h3 id="calloutpy数值" data-anchor-id="g5gq">callout.py（数值）</h3>

<pre data-anchor-id="9ewj"><code>import os,sys

print 'Sensor:', str(sys.argv[1])
print 'Value:', str(sys.argv[2])
print 'key:', str(sys.argv[3])
</code></pre>

<p data-anchor-id="zyli">在需要的数值传感器下填入callout.py和条件,数值发生改变时条件若满足，会发送debug信息到log里。</p>

<div class="md-section-divider"></div>

<h3 id="sendemailpy数值" data-anchor-id="drlv">send_email.py（数值）</h3>

<p data-anchor-id="kr0r">发邮件到指定邮箱，需要修改账号密码，详见sh/callback</p>

<div class="md-section-divider"></div>

<h1 id="others" data-anchor-id="95f5">others</h1>

<p data-anchor-id="5pfr">Any issues or improvements please contact jacob-chen@iotwrt.com</p>







            </div><!-- /.box-body -->

        </div><!-- /.box -->
    </div>
</div>
{% endblock %}